package cn.followtry.zk.example.lock;

import java.util.concurrent.TimeUnit;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;

/**
 * Created by followtry on 2017/5/31.
 */
public class ExampleClientThatLocks {
  private final InterProcessMutex lock;
  private final FakeLimitedResource resource;
  private final String clientName;
  
  public ExampleClientThatLocks(CuratorFramework client,String lockPath,FakeLimitedResource resource,String clientName)
  {
    this.resource = resource;
    this.clientName = clientName;
    lock = new InterProcessMutex(client, lockPath);
  }
  
  public void doWork(long time, TimeUnit unit) throws Exception
  {
    if ( !lock.acquire(time, unit) )
    {
      throw new IllegalStateException(clientName + " could not acquire the lock");
    }
    try
    {
      System.out.println(clientName + " has the lock");
      resource.use();
    }
    finally
    {
      System.out.println(clientName + " releasing the lock");
      lock.release(); // always release the lock in a finally block
      System.out.println();
    }
  }
}
